package com.adam.management.service.impl;

import com.adam.management.mapper.PermissionMapper;
import com.adam.management.model.UserDTO;
import com.adam.management.service.UserService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

/**
 * @projectName: adam-management-system
 * @description:
 * @author: Adam
 * @create: 2019-11-30 15:59
 **/
@Configuration
public class UserDetailServiceImpl implements UserDetailsService {

    private static Logger logger = LoggerFactory.getLogger(UserDetailServiceImpl.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDTO userDb = userService.findUserByUserId(username);
        userDb.setPassword(this.passwordEncoder.encode(userDb.getPassword()));
        //由于权限参数不能为空，所以这里先使用AuthorityUtils.commaSeparatedStringToAuthorityList方法模拟一个admin的权限，
        // 该方法可以将逗号分隔的字符串转换为权限集合。
        //return new User(userDb.getUsername(),userDb.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        //return new User(s,this.passwordEncoder.encode("admin"),AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        // 模拟一个用户，替代数据库获取逻辑
        //MyUser user = new MyUser();
        //user.setUserName(username);
        //user.setPassword(this.passwordEncoder.encode("123456"));
        // 输出加密后的密码
        //logger.info("===>>> 加密后的密码为=[{}]",user.getPassword());

        List<GrantedAuthority> authorities;
        String permissionStr = permissionMapper.selectUserPerm(username);
        authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(permissionStr);
       /* if (StringUtils.equalsIgnoreCase("admin", username)) {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        } else {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("test");
        }*/
        logger.info("==>>> 权限=[{}]", JSON.toJSONString(authorities));
        //return new User(username, user.getPassword(), user.isEnabled(),
        // user.isAccountNonExpired(), user.isCredentialsNonExpired(),
           //     user.isAccountNonLocked(), authorities);
        return new User(username,userDb.getPassword(),authorities);
    }


}
